package com.rs.adminweb.reserve.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;

import com.jaxplat.commons.Row;
import com.rs.commons.web.AdminBaseAction;
import com.rs.rsbiz.reserve.delegate.ReserveMgrDelegate;
import com.rs.rsbiz.services.ReserveFacade;
import com.rs.rsbiz.services.ReserveFactory;

public class ReserveMgrAction extends AdminBaseAction
{
    private Log log = LogFactory.getLog(getClass());
    private ReserveMgrDelegate reserveMgrDelegate;

    public ReserveMgrDelegate getReserveMgrDelegate()
    {
        return reserveMgrDelegate;
    }

    public void setReserveMgrDelegate(ReserveMgrDelegate reserveMgrDelegate)
    {
        this.reserveMgrDelegate = reserveMgrDelegate;
    }

    public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row param = this.parseRequestParametersToRow(request);
        Object[] result = this.getReserveMgrDelegate().getOrderList(param);

        JSONObject json = new JSONObject();
        // 这里必须写死为rows和total
        // 否则页面不显示
        json.element("total", ((Integer) result[1]).intValue());
        json.element("rows", ((List) result[0]));

        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().print(json.toString());
        response.getWriter().flush();
        response.getWriter().close();
        return null;

    }

    public ModelAndView main(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row param = this.parseRequestParametersToRow(request);

        Map attrs = new HashMap();
        return new ModelAndView("list", attrs);
    }

    public ModelAndView view(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row param = this.parseRequestParametersToRow(request);
        Row result = this.getReserveMgrDelegate().viewOrder(param);

        Map args = new HashMap();
        args.putAll(result);
        return new ModelAndView("view", args);

    }

    public ModelAndView refund(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row param = this.parseRequestParametersToRow(request);

        long orderId = param.getLong("order_id");
        ReserveFacade facade = ReserveFactory.getReserveFacade();
        Row result = facade.refund(param);
        JSONObject json = new JSONObject();
        // 这里必须写死为rows和total
        // 否则页面不显示
        json.putAll(result);
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().print(json.toString());
        response.getWriter().flush();
        response.getWriter().close();
        return null;

    }
}
